package cn.tsmchina.one.dao.rproduct;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.ScrollableResults;
import org.springframework.stereotype.Repository;

import cn.tsmchina.one.dao.BaseDaoSupport;
import cn.tsmchina.one.dao.ResultMapping;
import cn.tsmchina.one.domain.rproduct.Rproduct;
import cn.tsmchina.one.util.C;

@Repository
public class RproductDaoImpl extends BaseDaoSupport<Rproduct> implements RproductDao
{

	@Override
	public Long addProduct(Rproduct product)
	{ 
		return (Long) add(product);
	}
	
	@Override
	public void deleteById(Long id) {
		
		super.delete(id);
	}
	
	@Override
	public void updateProduct(Rproduct product) {
		
		super.update(product);
	}

	@Override
	public List<Rproduct> getQueryOrder(int page, long customerId)
	{
		String hql = "from Rproduct  order by id desc";
		return super.lists(hql, page, C.Paging.SIZE);
	}

	@Override
	public int total(long customerId)
	{
		String hql = "select count(*) from Rproduct p where 1=1";
		return count(hql);
	}

	@Override
	public List<Rproduct> getProductsByOrderId(long orderId)
	{
		return null;
	}

	@Override
	public Rproduct getProduct(long id)
	{
		return super.findById(id);
	}

	@Override
	public List<Rproduct> getActivitys()
	{
		String hql = "select p.id,p.productName,p.price,p.images from Product p where p.menuId = 2";
		List<Rproduct> list = super.scroll(hql, new ResultMapping<Rproduct>() {
			@Override
			public List<Rproduct> scroll(ScrollableResults scroll)
			{
				List<Rproduct> list = new ArrayList<>();
				Rproduct product = null;
				while(scroll.next())
				{
					product = new Rproduct();
					product.setId(scroll.getLong(0));
					product.setProductName(scroll.getString(1));
					product.setPrice(scroll.getDouble(2));
					product.setImages(scroll.getString(3));
					list.add(product);
					product = null;
				}
				return list;
			}
		});
		return list;
	}

	@Override
	public List<Rproduct> getFoodsByCategory(int cid, int page) {
		/*暂时这么搞*/
		String categoryName = "";
		if(1 == cid){
			categoryName = "水果";
		} else if (2 == cid){
			categoryName = "农产品";
		} else if (3 == cid){
			categoryName = "养生类";
		}
		String hql = "from Rproduct p where p.category = ? order by p.price";
		return super.lists(hql, page, C.Paging.RED_PAGE_SIZE, new Object[]{categoryName});
		/**/
//		String hql = "from Product p where p.category = ? order by p.price";
//		return super.lists(hql, page, C.Paging.FRONT_PAGE_SIZE, new Object[]{cid});
	}

	@Override
	public List<Rproduct> getFoodsByCategoryName(String name, int page) {
		String hql = "from Rproduct p where p.category = ? order by p.price";
		return super.lists(hql, page, C.Paging.RED_PAGE_SIZE, new Object[]{name});
	}
	
	
	
	@Override
	public int getFoodsCountByCategory(int cid) {
		/*暂时这么搞*/
		String categoryName = "";
		if(1 == cid){
			categoryName = "水果";
		} else if (2 == cid){
			categoryName = "农产品";
		} else if (3 == cid){
			categoryName = "养生类";
		}
		String hql = "select count(*) from Rproduct p where p.category = ?";
		//return super.count(hql, new Object[]{cid});
		return super.count(hql, new Object[]{categoryName});
	}

	@Override
	public int getFoodsCountByCategoryName(String name) {
		String hql = "select count(*) from Rproduct p where p.category = ?";
		return super.count(hql, new Object[]{name});
	}	
	
	@Override
	public List<Rproduct> getProducts(long customerId) {
		// TODO Auto-generated method stub
		String hql = "from Rproduct";
		return super.lists(hql, new Object[]{});
	}

	@Override
	public List<Rproduct> getAllFoodsByCategory(int cid) {
		String hql = "from Rproduct p where p.category = ? order by p.id desc";
		return super.lists(hql, new Object[]{cid});
	}
}
